<?php

class Admin {
	
	public function Admin(){
		
	}
	
	public function totalUsuario(){
		return  $this->countTotal("usuario");
	}
	
	public function totalTarefas($where="1"){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT COUNT(*) AS total,
						SUM(IF(concluida = 1,1,0))  concluida,
						SUM(IF(concluida != 1,1,0))  fazer
					FROM {$this->db}.tarefa
					WHERE
					  ".$where;
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				$row = $data->fetch();
				return $row;
			}
			else{
				return  0;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  0;
		}
	}
	
	
	public function totalCategoria(){
		return  $this->countTotal("categoria");
	}
	
	protected function countTotal($table){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT COUNT(*) AS total
					FROM {$this->db}.".$table."
					WHERE
					  1";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				$row = $data->fetch();
				return $row["total"];
			}
			else{
				return  0;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  0;
		}
	}


	public function countWhereTotal($table,$where="1"){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT COUNT(*) AS total
					FROM {$this->db}.".$table."
					WHERE
					  ".$where;
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				$row = $data->fetch();
				return $row["total"];
			}
			else{
				return  0;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  0;
		}
	}
	
	
	public function listaUsuarios(){
		try {
			$Conexao = Conexao::Conect();
			$sql="SELECT
						u.nome,
						u.id,
						(SELECT COUNT(tf.id)
							FROM 
								 {$this->db}.tarefas_feitas tf
							WHERE
								tf.usuario = u.id 
						) as feitas,
						(SELECT COUNT(tl.id)
							FROM 
								 {$this->db}.tarefas_lidas tl
							WHERE
								tl.usuario = u.id 
						) as lidas
					FROM
						 relatorio.usuario u
					WHERE
						1
					ORDER BY nome ASC;";
			$data = $Conexao->query ( $sql );
			if ( $data->rowCount() > 0) {
				return $data->fetchAll();
			}
			else{
				return  false;
			}
		} catch ( PDOException $e ) {
			echo 'ERROR: ' . $e->getMessage ();
			return  false;
		}
	}
	
	
	
}

